// pages/searchProblem/searchProblem.js
const app = getApp()
const lbbService = require('../service/service.js')

Page({

  /**
   * 页面的初始数据
   */
  data: {
    provIndex: 0,
    cityIndex: 0,
    hosIndex: 0,
    manIndex: 0,
    masterUid: 0,
    handleType: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      scrollHeight: app.getScrollHeight(110)
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getCitys()
    this.getMembers()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },
  getMembers: function(){
    let that = this
    lbbService.getMembers({},function(res){
      that.setData({
        members:res.data
      })
      that.setMembers()
    })
  },
  getCitys: function () {
    let that = this
    lbbService.getCitys({}, function (res) {
      that.setData({
        propArray: res
      })
      let provList = ['全部']
      that.data.propArray.forEach(item => {
        provList.push(item.provName)
      })
      that.setData({
        provList: provList
      })
      that.setIdArray()
    })
  },
  typeChange: function(e){
    let that = this
    that.setData({
      handleType: e.detail.value
    })
    that.getProblemList()
  },
  provChange: function (e) {
    let that = this
    let index = e.detail.value
    that.setData({
      provIndex: index,
      cityIndex: 0
    })
    that.setIdArray()
  },
  cityChange: function (e) {
    let that = this
    let index = e.detail.value
    that.setData({
      cityIndex: index,
      hosIndex:0
    })
    that.setIdArray()
  },
  hosChange: function (e) {
    let that = this
    let index = e.detail.value
    that.setData({
      hosIndex: index
    })
    that.getProblemList()
  },
  manChange: function (e) {
    let that = this
    let index = e.detail.value
    that.setData({
      manIndex: index
    })
    if(that.data.manIndex>0){
      that.setData({
        masterUid: that.data.members[that.data.manIndex - 1].masterUid
      })
    }else {
      that.setData({
        masterUid: 0
      })
    }
    that.getProblemList()
  },
  setCityList: function () {
    let that = this
    let cityList = ['全部']
    if(that.data.provIndex>0){
      that.data.propArray[that.data.provIndex - 1].cityList.forEach(item => {
        cityList.push(item.cityName)
      })
    }
    that.setData({
      cityList: cityList
    })
    let hosList = ['全部']
    that.data.hospitalList.forEach(item => {
      hosList.push(item.hospitalName)
    })
    that.setData({
      hosList: hosList
    })
    that.getProblemList()
  },
  getHospitalList: function () {
    let that = this
    let selectorArray = that.data.selectorArray
    lbbService.getHospitalList({ array: selectorArray }, function (res) {
      that.setData({
        hospitalList: res
      })
      that.setCityList()
      // that.getProblemList()
    })
  },
  setMembers: function(){
    let that = this
    let members = that.data.members
    let membersName = ['全部']
    members.forEach(item => {
      membersName.push(item.masterName)
    })
    that.setData({
      membersName: membersName
    })
  },
  setIdArray: function () {
    let that = this
    let provId = 0
    if (that.data.provIndex > 0) {
      provId = that.data.propArray[that.data.provIndex - 1].id
    }
    let cityId = 0
    if (that.data.cityIndex > 0) {
      if (that.data.provIndex > 0){
        cityId = that.data.propArray[that.data.provIndex - 1].cityList[that.data.cityIndex - 1].cityId
      }else {
        cityId = that.data.propArray[that.data.provIndex].cityList[that.data.cityIndex - 1].cityId
      }
    }
    let selectorArray = [provId, cityId, 0]
    that.setData({
      selectorArray: selectorArray
    })
    that.getHospitalList()
  },
  getProblemList: function () {
    let that = this
    let hospitalId = 0
    if (that.data.hosIndex > 0) {
      hospitalId = that.data.hospitalList[that.data.hosIndex - 1].id
    }
    lbbService.queryProblems({
      handleType: that.data.handleType,//0 待处理,1 已处理
      HandleUserId: that.data.masterUid,//负责人的id
      // HandleUserId: '464458',//李登科的id
      hospitalId: hospitalId,
      provinceId: that.data.selectorArray[0],
      cityId: that.data.selectorArray[1],
    }, function (res) {
      that.setData({
        problemList: res.data
      })
    })
  },
  getMore: function () {
    let that = this
    console.log('getMore>> ')
    let page_num = that.data.page_num + 1
    that.setData({
      page_num: page_num
    })
    that.getProblemList()
  },
})